home *** CD-ROM | disk | FTP | other *** search
/ ETO Development Tools 2 / ETO Development Tools 2.iso / Essentials / Developer Essentials Nov 90 / Apple II / Apple.II.partition / Tools / DTS.Samples / SC17Darts / Darts.p / UEvent.inc.p < prev    next >
Encoding:
Text File  |  1990-06-25  |  9.8 KB  |  292 lines  |  [TEXT/pdos]

  1. {**********************************************************************
  2. {*
  3. {* Darts uEvent -- Version 3.0  (implemenation)
  4. {*
  5. {* Copyright (c)
  6. {* Steven E. Glass and Apple Computer, Inc.  1986-1989
  7. {* All Rights Reserved.
  8. {*
  9. {* Developer Technical Support Apple II Sample Code
  10. {*
  11. {* This file contains the code which implements the 
  12. {* main event loop used by the program.
  13. {*
  14. {**********************************************************************}
  15. {$R-}
  16.  
  17. var
  18.     lastWindow   : GrafPortPtr;     { This private global is used in checkFrontW
  19.                                       to prevent extra work when the windows
  20.                                       have not changed.  It is initialized
  21.                                       at the beginning of mainEvent.
  22.                                     }
  23.  
  24.  
  25. {**********************************************************************************}
  26. {*
  27. {* doScore
  28. {*
  29. {* This procedure does all the scoring in this program.  It is only called when
  30. {* the user presses a button that has a non-zero low word for an ID.
  31. {*
  32. {* How the scoreing works depends on the game being played.  There is a section
  33. {* for scoring each of the games.
  34. {*
  35. {**********************************************************************************}
  36. procedure   doScore(playerNum : integer;  amount : integer);
  37.     var
  38.         otherPlayer,
  39.         playerHits,
  40.         otherHits,
  41.         i,j, junk       : integer;
  42.  
  43.     begin
  44.         if gameType = 0 then
  45.             begin
  46.                 score[playerNum] := score[playerNum] + amount;
  47.                 addToList(playerNum, amount);
  48.                 invalScore(playerNum);
  49.             end
  50.         else
  51.             begin
  52.                 if playerNum = Player1 then otherPlayer := Player2
  53.                 else otherPlayer := Player1;
  54.                 
  55.                 playerHits := crickettTables[playerNum][amount];
  56.                 otherHits  := crickettTables[otherPlayer][amount];
  57.  
  58.                 if ((playerHits < 3) or (otherHits < 3)) then
  59.                     begin       { At least one player is open. }
  60.                         playerHits := playerHits + 1;
  61.                         crickettTables[playerNum][amount] := playerHits;
  62.                         fixButtonTitle(playerNum, amount);
  63.  
  64.                         if ((playerHits > 3) and (otherHits < 3)) then
  65.                             begin
  66.                                 score[playerNum] := score[playerNum] + amount;
  67.                                 invalScore(playerNum);
  68.                             end;
  69.  
  70.                         addToList(playerNum, amount);
  71.                     end;
  72.                     
  73.                 if not weHaveAWinner then
  74.                     begin
  75.                         i := 0;
  76.                         while i < 7 do
  77.                             begin
  78.                                 j := i + 15;
  79.                                 if (i = 6) then j := 25;
  80.  
  81.                                 if (crickettTables[playerNum][j] < 3) then
  82.                                         i := 7;
  83.                                 i := i + 1;
  84.                             end;
  85.                             
  86.                         { i is 7 when all are closed for this player. }
  87.                         if (i = 7) and (score[playerNum] >= score[otherPlayer]) then
  88.                             begin
  89.                                 weHaveAWinner := TRUE;
  90.                                 BeginUpdate(theWindow);
  91.                                 drawThisWindow;
  92.                                 EndUpdate(theWindow);
  93.                                 junk := AlertWindow(RefIsResource * 2,
  94.                                             NIL,
  95.                                             Ptr(3));
  96.                             end;
  97.                     end;
  98.             end;
  99.     end;
  100.  
  101.  
  102.  
  103. {**********************************************************************************}
  104. {*
  105. {* DoControls
  106. {*
  107. {* This procedure is called by MainEvent whenever taskmaster returns inControl.
  108. {*
  109. {**********************************************************************************}
  110. procedure doControls;
  111.     var
  112.         whichButton : integer;
  113.         buttonNum : integer;
  114.         lastItem : integer;
  115.         playerNum : integer;
  116.         
  117.     begin
  118.  
  119.         whichButton := event.wmTaskData4;
  120.         
  121.         { If the low word is not 0 its a scoring control }
  122.         if WhichButton <> 0 then
  123.             begin
  124.                 if 0 = BAND(whichButton,$8000) then playerNum := Player1
  125.                 else playerNum := Player2;
  126.         
  127.                 whichButton := event.wmTaskData4;
  128.                 whichButton := BAND(whichButton,$7FFF);
  129.                 
  130.                 
  131.                 doScore(playerNum,whichButton);
  132.             end
  133.         { If the low word is zero, its not a scoring control }
  134.         else
  135.             begin
  136.                 whichButton := HiWord(event.wmTaskData4);
  137.                 if whichButton = 2 then doNewItem
  138.                 else if whichButton = 1 then 
  139.                     begin
  140.                         buttonNum := AlertWindow(RefIsResource*2,NIL,ptr(ConfirmTextID));
  141.                         if buttonNum = 1 then 
  142.                             doQuitItem;
  143.                     end
  144.                 else if whichButton = 3 then
  145.                     begin
  146.                         if event.wmClickCount > 1 then 
  147.                             begin
  148.                                 removeSelected(Player1);
  149.                                 event.wmClickCount := 0;
  150.                             end;
  151.                     end
  152.                 else if whichButton = 4 then
  153.                     begin
  154.                         if event.wmClickCount > 1 then 
  155.                             begin
  156.                                 removeSelected(Player2);
  157.                                 event.wmClickCount := 0;
  158.                             end;
  159.                     end;
  160.             end;
  161.         
  162.     end;
  163.  
  164.  
  165.  
  166.  
  167. {**********************************************************************************}
  168. {*
  169. {* checkFrontW
  170. {*
  171. {* This routine adjusts the system menu bar depending on what window is selected.
  172. {* It activates the edit menu if a desk accessory is opened and selected.  It
  173. {* deactivates the edit menu if the game window becomes active again.
  174. {*
  175. {* We do this because it makes no sense to have progam menus active when a desk
  176. {* accessory is active and there is no sense having the edit menu active when
  177. {* this program does not use it.
  178. {*
  179. {* It is called every time through the event loop.  The global variable lastWindow
  180. {* helps us from making lots of checks if nothing has changed.
  181. {*
  182. {**********************************************************************************}
  183. procedure CheckFrontW;
  184.  
  185.     var 
  186.         theWindow    : GrafPortPtr;
  187.  
  188.  
  189.  
  190.     procedure DisableItems;
  191.  
  192.     {Private routine to disable (dim) certain menu titles}
  193.  
  194.         begin   {of DisableItems}
  195.             DisableMItem (NewItem);
  196.             DisableMItem (QuitItem);
  197.             DisableMItem (AboutItem);
  198.         end;    {of DisableItems}
  199.  
  200.  
  201.  
  202.     procedure EnableItems;
  203.  
  204.     {Private routine to enable (undim) certain menu titles}
  205.  
  206.         begin   {of EnableItems}
  207.             EnableMItem (NewItem);
  208.             EnableMItem (QuitItem);
  209.             EnableMItem (AboutItem);
  210.         end;    {of EnableItems}
  211.  
  212.  
  213.  
  214.     begin   {of CheckFrontW}
  215.         theWindow := FrontWindow;
  216.         
  217.         { Has anything changed? }
  218.         if theWindow = lastWindow then Exit(CheckFrontW);
  219.         
  220.         { Something's changed so lets fix this stuff }
  221.         if theWindow = nil then 
  222.             begin
  223.                 SetMenuFlag ($0080,EditMenuID); { disable the edit menu }
  224.                 DrawMenuBar;
  225.                 DisableItems;
  226.             end
  227.         else 
  228.             begin
  229.                 if GetSysWFlag (theWindow) <> false then 
  230.                     begin
  231.                         DisableItems;                       { Disable application items }
  232.                         SetMenuFlag ($FF7F,EditMenuID);     { Enable the edit menu. }
  233.                         SetMenuFlag ($0080,GameMenuID);     { disable the game menu }
  234.                         DrawMenuBar;                        { Redraw after fixing above }
  235.                     end
  236.                 else  
  237.                     begin   
  238.                         SetMenuFlag ($0080,EditMenuID);     { disable the edit menu }
  239.                         SetMenuFlag ($FF7F,GameMenuID);     { enable the game menu }
  240.                         DrawMenuBar;                        { Redraw after fixing above }
  241.                         EnableItems;                        { Enable application items }
  242.                     end;
  243.             end;
  244.         
  245.         { Remember theWindow for next time. }
  246.         lastWindow := theWindow;
  247.     end;    {of CheckFrontW}
  248.  
  249.  
  250.  
  251.  
  252.  
  253. {**********************************************************************************}
  254. {*
  255. {* MainEvent
  256. {*
  257. {* This is the main routine in the program.  It continuously calls taskmaster and
  258. {* handles any events that occur.  It also keeps the right menus active by 
  259. {* calling checkFrontW.
  260. {*
  261. {**********************************************************************************}
  262. procedure MainEvent;
  263.  
  264.  
  265.     var 
  266.         code : integer;
  267.         checkFirstUpdate : boolean;
  268.     
  269.     begin   {of MainEvent}
  270.         event.wmTaskMask := $001FFFFF;  {Allow TaskMaster to do everything}
  271.         quitFlag             := false;      {Done flag will be set by Quit item}
  272.         checkFirstUpdate := true;
  273.         lastWindow := NIL;
  274.         
  275.         repeat
  276.             CheckFrontW;
  277.             code := TaskMaster ($FFFF,event);
  278.             case code of
  279.                 wInSpecial,
  280.                 wInMenuBar      : doMenu;
  281.                 wInControl      : doControls;
  282.                 otherwise 
  283.                     if checkFirstUpdate then
  284.                         if firstUpdateComplete then
  285.                             begin
  286.                                 InitCursor;
  287.                                 checkFirstUpdate := false;
  288.                             end;
  289.             end;
  290.         until quitFlag;
  291.     end;    {of MainEvent}
  292.